/*
 * TermSequentialFile_gtest.h
 *
 *  Created on: Nov 27, 2012
 *      Author: bruno
 */

#include <gtest/gtest.h>
#include <memory>
#include <vector>
#include "../physical_layer/file_system/TermSequentialFile.h"
#include "../physical_layer/file_system/Term.h"

using std::vector;
using physical::Term;
using physical::TermSequentialFile;

/**
 * Test Suite de la clase TermSequentialFile
 */
class TermSequentialFileTestFixture: public ::testing::Test {

public:

	std::string m_filename;

	TermSequentialFileTestFixture() {
		const ::testing::TestInfo* const test_info =
				::testing::UnitTest::GetInstance()->current_test_info();

		std::string filename(test_info->name());
		filename.append(".");
		filename.append(test_info->test_case_name());
		filename.append(".tmp");

		m_filename = filename;
	}

	virtual ~TermSequentialFileTestFixture() {
	}

	void SetUp() {
	}

	void TearDown() {
	}

	const char* getFileNameForTest() {
		return m_filename.c_str();
	}
};

/**
 * Test Add and retrieve one term of the terms sequential file
 *
 *
 */TEST_F( TermSequentialFileTestFixture, PutAndGetTerm) {

	 TermSequentialFile termsFile;

	termsFile.open(getFileNameForTest(),
			std::ios_base::in | std::ios_base::out | std::ios_base::trunc);

	Term term("Term1");

	termsFile.put(term);

	termsFile.close();

	termsFile.open(getFileNameForTest(),
			std::ios_base::in | std::ios_base::out);

	Term retrievedTerm = termsFile.get();

	ASSERT_TRUE(retrievedTerm == term);
}

/**
 * Test hasNext term of the terms sequential file
 *
 *
 */TEST_F( TermSequentialFileTestFixture, HasNextTerm) {

	 TermSequentialFile termsFile;

	termsFile.open(getFileNameForTest(),
			std::ios_base::in | std::ios_base::out | std::ios_base::trunc);

	Term term1("Term1");

	ASSERT_FALSE(termsFile.hasNext());

	termsFile.put(term1);

	ASSERT_TRUE(termsFile.hasNext());

	termsFile.close();

	termsFile.open(getFileNameForTest(),
			std::ios_base::in | std::ios_base::out);

	termsFile.put(term1);

	ASSERT_TRUE(termsFile.hasNext());
}

/**
 * Test Add and retrieve several terms of the terms sequential file
 *
 *
 */TEST_F( TermSequentialFileTestFixture, PutAndGetSeveralTerms) {

	 TermSequentialFile termsFile;

	termsFile.open(getFileNameForTest(),
			std::ios_base::in | std::ios_base::out | std::ios_base::trunc);

	Term term1("Term1");

	termsFile.put(term1);

	ASSERT_TRUE(termsFile.hasNext());

	Term term2("Term2");

	termsFile.put(term2);

	Term term3("Term3");

	termsFile.put(term3);

	termsFile.close();

	termsFile.open(getFileNameForTest(),
			std::ios_base::in | std::ios_base::out);

	Term retrievedTerm = termsFile.get();

	ASSERT_TRUE(retrievedTerm == term1);

	ASSERT_TRUE(termsFile.hasNext());

	retrievedTerm = termsFile.get();

	ASSERT_TRUE(retrievedTerm == term2);

	retrievedTerm = termsFile.get();

	ASSERT_TRUE(retrievedTerm == term3);
}

